package com.tyk.bookstore.back.member.controller;


import com.tyk.bookstore.back.common.core.model.page.PageVo;
import com.tyk.bookstore.back.common.core.model.result.R;
import com.tyk.bookstore.back.member.model.query.DeliveryAddressAddQuery;
import com.tyk.bookstore.back.member.model.query.DeliveryAddressPageQuery;
import com.tyk.bookstore.back.member.model.query.DeliveryAddressUpdateQuery;
import com.tyk.bookstore.back.member.model.vo.DeliveryAddressPageVo;
import com.tyk.bookstore.back.member.model.vo.DeliveryAddressVo;
import com.tyk.bookstore.back.member.service.DeliveryAddressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping({"/member/deliveryAddress"})
@Deprecated
@RestController
@Tag(name = "送货地址管理", description = "送货地址管理")
public class DeliveryAddressController {

    private final DeliveryAddressService deliveryAddressService;

    public DeliveryAddressController(final DeliveryAddressService deliveryAddressService) {
        this.deliveryAddressService = deliveryAddressService;
    }

    @GetMapping({"{id}"})
    @Operation(summary = "获取送货地址详情", description = "获取送货地址详情")
    public R<DeliveryAddressVo> getDeliveryAddressById(@PathVariable @NotNull @Min(1) Long id) {
        return R.ok(this.deliveryAddressService.getDeliveryAddressById(id));
    }

    @GetMapping({"list"})
    @Operation(summary = "分页查询送货地址", description = "分页查询送货地址")
    public R<DeliveryAddressPageVo> getDeliveryAddressList(@Validated DeliveryAddressPageQuery query, BindingResult bindingResult) {
        return bindingResult.hasErrors() ? R.ok(PageVo.instance(DeliveryAddressPageVo.class)) : R.ok(this.deliveryAddressService.getDeliveryAddressPage(query));
    }

    @PostMapping
    @Operation(summary = "添加送货地址", description = "添加送货地址")
    public R<Object> addDeliveryAddress(@Validated @RequestBody DeliveryAddressAddQuery query) {
        this.deliveryAddressService.addDeliveryAddress(query);
        return R.ok();
    }

    @PutMapping
    @Operation(summary = "更新送货地址", description = "更新送货地址")
    public R<Object> updateDeliveryAddressById(@Validated @RequestBody DeliveryAddressUpdateQuery query) {
        this.deliveryAddressService.updateDeliveryAddressById(query);
        return R.ok();
    }

    @DeleteMapping({"{id}"})
    @Operation(summary = "根据ID删除送货地址", description = "根据ID删除送货地址")
    public R<Object> deleteDeliveryAddressById(@PathVariable @NotNull @Min(1) Long id) {
        this.deliveryAddressService.deleteDeliveryAddressById(id);
        return R.ok();
    }

    @DeleteMapping
    @Operation(summary = "根据ID批量删除送货地址", description = "根据ID批量删除送货地址")
    public R<Object> deleteDeliveryAddressByIds(@NotNull @NotEmpty List<Long> ids) {
        this.deliveryAddressService.deleteDeliveryAddressByIds(ids);
        return R.ok();
    }
}
